// import util from 'util.js';
/**
 * url 请求地址
 * success 成功的回调
 * fail 失败的回调
 */
function _get(url, success, fail) {

  console.log("start_get.url: " + url);
  wx.request({
    url: url,
    header: {
      'Content-Type': 'application/json;charset=UTF-8'
    },
    success: function (res) {
      console.log(res);
      success(res);
    },
    fail: function (res) {
      console.log(res);
      fail(res);
    }
  });

  console.log("----end-----_get----");
}

/**
 * url 请求地址
 * success 成功的回调
 * fail 失败的回调
 */
function _post_form(url, data, success, fail) {
  console.log("----_post--start-------");
  wx.request({
    url: url,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    method: 'POST',
    data: { data: data },
    success: function (res) {
      success(res);
    },
    fail: function (res) {
      fail(res);
    }
  });
  console.log("----end-----_get----");
}

/**
* url 请求地址
* success 成功的回调
* fail 失败的回调
*/
function _post_json(url, data, success, fail) {
  console.log("----_post--start-------");
  wx.request({
    url: url,
    header: {
      'content-type': 'application/json',
    },
    method: 'POST',
    data: data,
    success: function (res) {
      console.log(res);
      success(res);
    },
    fail: function (res) {
      console.log(res);
      fail(res);
    }
  });

  console.log("----end----_post-----");
}
function cget(url, that){
  var result = {};
  this._get(url,
    function (res) {
      if (res.statusCode == 200) {
        if (res.data.code == 200) {
          wx.showToast({
            title: '获取' + that.data.pageName + '成功(' + res.data.code + ')',
          })
          //成功
          // that.setData({ objName: res.data.data[0] });
          console.log("util.res: " + JSON.stringify(res.data.data[0]));
          // console.log("that.payDetail: " + JSON.stringify(that.data.payDetail));
          result = res.data.data[0];
        } else {
          //接口错误
          wx.showToast({
            title: '获取' + that.data.pageName + '出错(' + res.data.code + ')',
          })
        }
      } else {
        // wx.hideToast();
        //系统错误
        wx.showToast({
          title: '获取' + that.data.pageName + '发生系统错误(' + res.statusCode + ')',
        })
      }
    });

  console.log("util.result: " + JSON.stringify(result));
    return result;
}
module.exports = {
  _get: _get,
  // cget: cget,
  _post: _post_form,
  _post_json: _post_json
}